import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:location/location.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:intl/intl.dart';
import 'package:xiaomiao/pages/album_page.dart';
import 'package:xiaomiao/pages/dashboard_page.dart';
import 'package:xiaomiao/pages/diary_page.dart';
import 'package:xiaomiao/pages/memories_page.dart';
import 'package:xiaomiao/pages/tools_page.dart';
import 'package:xiaomiao/widgets/animated_background.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized(); // 确保WidgetsFlutterBinding初始化

  // 设置状态栏透明和亮色主题（内容为黑色）
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
    statusBarColor: Colors.transparent, // 设置状态栏颜色为透明
    statusBarIconBrightness: Brightness.dark, // Android 设置状态栏图标为深色
    statusBarBrightness: Brightness.light, // iOS 设置状态栏图标为深色
  ));
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // 樱花粉色主题
    final sakuraPink = MaterialColor(
      0xFFFFC0CB, // 基础樱花粉色
      <int, Color>{
        50: const Color(0xFFFFF0F3),
        100: const Color(0xFFFFE0E6),
        200: const Color(0xFFFFCCD5),
        300: const Color(0xFFFFB7C5),
        400: const Color(0xFFFFA8B9),
        500: const Color(0xFFFFC0CB), // 主色调
        600: const Color(0xFFFF9AAD),
        700: const Color(0xFFFF8DA3),
        800: const Color(0xFFFF7F98),
        900: const Color(0xFFFF6B88),
      },
    );

    return MaterialApp(
      title: '蜡笔今天开心',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: sakuraPink,
        visualDensity: VisualDensity.adaptivePlatformDensity,
        appBarTheme: AppBarTheme(
          backgroundColor: sakuraPink,
          elevation: 0,
        ),
        floatingActionButtonTheme: FloatingActionButtonThemeData(
          backgroundColor: sakuraPink,
        ),
        elevatedButtonTheme: ElevatedButtonThemeData(
          style: ElevatedButton.styleFrom(
            backgroundColor: sakuraPink,
            foregroundColor: Colors.white,
          ),
        ),
        textButtonTheme: TextButtonThemeData(
          style: TextButton.styleFrom(
            foregroundColor: sakuraPink,
          ),
        ),
        switchTheme: SwitchThemeData(
          thumbColor: MaterialStateProperty.resolveWith<Color>((states) {
            if (states.contains(MaterialState.selected)) {
              return sakuraPink;
            }
            return Colors.grey;
          }),
          trackColor: MaterialStateProperty.resolveWith<Color>((states) {
            if (states.contains(MaterialState.selected)) {
              return sakuraPink.withOpacity(0.5);
            }
            return Colors.grey.withOpacity(0.5);
          }),
        ),
        checkboxTheme: CheckboxThemeData(
          fillColor: MaterialStateProperty.resolveWith<Color>((states) {
            if (states.contains(MaterialState.selected)) {
              return sakuraPink;
            }
            return Colors.grey;
          }),
        ),
        radioTheme: RadioThemeData(
          fillColor: MaterialStateProperty.resolveWith<Color>((states) {
            if (states.contains(MaterialState.selected)) {
              return sakuraPink;
            }
            return Colors.grey;
          }),
        ),
        colorScheme: ColorScheme.light(
          primary: sakuraPink,
          secondary: sakuraPink[300]!,
        ),
      ),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _selectedIndex = 0;

  final List<Widget> _pages = [
    const DashboardPage(),
    const MemoriesPage(),
    const AlbumPage(),
    const DiaryPage(),
    const ToolsPage(),
  ];

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: AnimatedBackground(
        child:_pages[_selectedIndex]),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.timeline),
            label: '回忆',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.photo_album),
            label: '相册',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.book),
            label: '日记',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.apps),
            label: '工具',
          ),
        ],
        currentIndex: _selectedIndex,
        selectedItemColor: Theme.of(context).primaryColor,
        unselectedItemColor: Colors.grey,
        onTap: _onItemTapped,
      ),
    );
  }
}

